package framework.spatial;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;

/**
 *
 * @author Lukáš Marek
 */
public class FRDSpatialGPoint extends FRDSpatialGItem
{
	private Point point;
	BufferedImage image = null;
	BufferedImage imageDefault = null;
	
	public FRDSpatialGPoint()
	{
		point = new Point();
		SelectedColor = new Color(0, 0, 255);
		imageDefault = new BufferedImage(24, 24, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = (Graphics2D) imageDefault.getGraphics();  
		g.setBackground(Color.WHITE);  
		g.clearRect(0, 0, 24, 24);  
	}
	
	public void setLocation(Point point)
	{
		this.point = point;
	}
	
	public Point getLocation()
	{
		return point;
	}
	
	@Override
	public void setPoints(Point p1, Point p2)
	{
		this.point.setLocation(mapPoint(p2));
	}
	
	@Override
	public boolean addPoints(Point p1, Point p2)
	{
		this.point.setLocation(mapPoint(p2));
		return true;
	}

	/**
	 * Nastaveni obrazku
	 * @param image 
	 */
	public void setImage(BufferedImage image)
	{
		this.image = image;
	}

	@Override
	public void draw(Graphics g)
	{
		BufferedImage img = image;
		if (image == null) img = imageDefault;
		Point p = showPoint(point);
		Point size = new Point((int)(img.getWidth()/3*back.getZoom()), (int)(img.getHeight()/3*back.getZoom()));
		g.drawImage(img, p.x - (int)(size.x / 2), p.y - (int)(size.y / 2), size.x, size.y, null);
		if (selected)
		{
			g.setColor(SelectedColor);
			g.drawRect(p.x - (int)(size.x / 2), p.y - (int)(size.y / 2), size.x, size.y);
		}
	}

	@Override
	public void drawEdit(Graphics g, Point p1, Point p2)
	{
		BufferedImage img = image;
		if (image == null) img = imageDefault;
		Point p = showPoint(point);
		Point size = new Point((int)(img.getWidth()/3*back.getZoom()), (int)(img.getHeight()/3*back.getZoom()));
		if (undermouse(p1)) g.drawImage(img, p.x - (int)(size.x / 2) + (p2.x - p1.x), p.y - (int)(size.y / 2) + (p2.y - p1.y), size.x, size.y, null);
	}
	
	@Override
	public void saveEdit(Point pt1, Point pt2)
	{
		Point mp1 = mapPoint(pt1);
		Point mp2 = mapPoint(pt2);
		point.x += mp2.x - mp1.x;
		point.y += mp2.y - mp1.y;
	}

	@Override
	public boolean undermouse(Point _p)
	{
		BufferedImage img = image;
		if (image == null) img = imageDefault;
		Point p = showPoint(point);
		Point size = new Point((int)(img.getWidth()/3*back.getZoom()), (int)(img.getHeight()/3*back.getZoom()));
		return (((p.x - size.x) <= _p.x) && (_p.x <= (p.x + size.x)) && ((p.y - size.y) <= _p.y) && (_p.y <= (p.y + size.y)));
	}
	
	/**
	 * prepocita bod z mapy na bod v panelu
	 * @param pt bod
	 * @return 
	 */
	private Point showPoint(Point pt)
	{
		double scale = back.getScale();
		Point zoomLoc = back.getPosition();
		double zoom = back.getZoom();
		double df = scale / zoom;
		Point ret = new Point();
		ret.x = (int)((pt.x - zoomLoc.x) / df);
		ret.y = (int)((pt.y - zoomLoc.y) / df);
		return ret;
	}
	
	/**
	 * prepocita bod z panelu na bod v mape
	 * @param pt
	 * @return 
	 */
	private Point mapPoint(Point pt)
	{
		double scale = back.getScale();
		Point zoomLoc = back.getPosition();
		double zoom = back.getZoom();
		double df = scale / zoom;
		Point ret = new Point();
		ret.x = (int)(pt.x * df) + zoomLoc.x;
		ret.y = (int)(pt.y * df) + zoomLoc.y;
		return ret;
	}
}
